/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useEffect, useState } from 'react';
import { Modal, Steps, Empty, Spin } from 'antd';
import { getLogisticsTrack } from '@/services/operatorManagement';
const { Step } = Steps;
const Index: React.FC<{
  isOpened: boolean;
  order_id: string;
  onCancel: () => void;
}> = (props) => {
  const { isOpened, onCancel, order_id } = props;
  const [data, setData] = useState<string[]>([]);
  const [loading, setLoading] = useState(false);
  const [stepCurrent, setStepCurrent] = useState(0);
  useEffect(() => {
    if (order_id) {
      setLoading(true);
      getLogisticsTrack({ order_id }).then((res) => {
        const { success, data: d } = res;
        setLoading(false);
        if (success) {
          setData(d);
          return;
        } else {
          setData([]);
        }
      });
    }
  }, [order_id]);
  const handleStepIndexChange = (current: any) => {
    setStepCurrent(current);
  };
  return (
    <Modal
      onCancel={onCancel}
      visible={isOpened}
      okButtonProps={{
        style: { display: 'none' },
      }}
      cancelButtonProps={{
        style: { display: 'none' },
      }}
    >
      {data.length ? (
        <Spin spinning={loading}>
          <Steps direction='vertical' current={stepCurrent} onChange={handleStepIndexChange}>
            {data.map((item) => {
              const title = item.substr(0, 19);
              const desc = item.substr(20);
              return <Step description={desc} title={title} />;
            })}
          </Steps>
        </Spin>
      ) : (
        <Empty description='暂无物流'></Empty>
      )}
    </Modal>
  );
};

export default Index;
